#include "AndroidVideoCaptureFactory.h"
#include "AndroidVideoCapture.h"

namespace simple_renderer{

const char* JAVA_VideoCaptureFactory = "com/vonchenchen/renderer/VideoCaptureFactory";

AndroidVideoCaptureFactory::AndroidVideoCaptureFactory(){

}

AndroidVideoCaptureFactory::~AndroidVideoCaptureFactory(){

}

#ifdef __ANDROID__
std::unique_ptr<VideoCapture> AndroidVideoCaptureFactory::CreateVideoCapture(){

    JNIEnv* env = AttachCurrentThreadIfNeeded();
    jclass cls = env->FindClass(JAVA_VideoCaptureFactory);
    jmethodID mid = env->GetStaticMethodID(cls, "createVideoCapture", "(ILandroid/content/Context;J)Lcom/vonchenchen/renderer/VideoCapture;");
    jint index = 0;
    jobject context = GetAndroidContext();
    auto capture = std::unique_ptr<AndroidVideoCapture>(new AndroidVideoCapture);

    jobject jcapture = env->CallStaticObjectMethod(cls, mid, index, context, reinterpret_cast<jlong>(capture.get()));

    auto jcapture_wrapper = std::unique_ptr<JavaGlobalRef<jobject>>(new JavaGlobalRef<jobject>(env, jcapture));
    capture->SetVideoCapture(jcapture_wrapper);

    return capture;
}
#endif

int AndroidVideoCaptureFactory::GetNumberOfCameras(){

    JNIEnv* env = AttachCurrentThreadIfNeeded();
    jclass cls = env->FindClass(JAVA_VideoCaptureFactory);
    jmethodID mid = env->GetStaticMethodID(cls, "getNumberOfCameras", "(Landroid/content/Context;)I");
    jobject context = GetAndroidContext();
    int ret = env->CallStaticIntMethod(cls, mid, context);

    return ret;
}

std::string AndroidVideoCaptureFactory::GetDeviceName(std::string id){

//    JNIEnv* env = AttachCurrentThreadIfNeeded();
//    jclass cls = env->FindClass(JAVA_VideoCaptureFactory);
//    jmethodID mid = env->GetStaticMethodID(cls, "getDeviceName", "(ILandroid/content/Context;)Ljava/lang/String;");
//    jobject context = GetAndroidContext();

    return "";
}

int AndroidVideoCaptureFactory::GetDeviceOrientation(std::string id){
    return 0;
}

std::string AndroidVideoCaptureFactory::GetCapabilities(std::string id){
    return "";
}
}